package cn.sjx.auth.controller;

import cn.sjx.auth.annotation.Permission;
import cn.sjx.auth.domain.Menu;
import cn.sjx.auth.domain.Menu;
import cn.sjx.auth.query.MenuQuery;
import cn.sjx.auth.service.MenuService;
import cn.sjx.basic.utils.AjaxResult;
import cn.sjx.basic.utils.PageList;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;

@Permission(name="菜单管理")
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Resource
    MenuService menuService;

    @Permission(name = "查询所有" ,descs = "查询所有菜单")
    @GetMapping
    @ApiOperation(value = "查询所有" )
    public AjaxResult getAll() {

        try {
            List<Menu> list = menuService.getAll();
            return AjaxResult.me().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败");
        }
    }

    @Permission(name = "新增/修改" )
    @PutMapping
    @ApiOperation(value = "新增或更新" )
    public AjaxResult insertOrupdate(@RequestBody Menu menu) {
        //通过Objects.isNull来判断
        try {
            if (Objects.isNull(menu.getId())) {
                menuService.insert(menu);
                return AjaxResult.me();
            } else {
                menuService.update(menu);
                return AjaxResult.me();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    @Permission(name = "删除" ,descs = "删除菜单")
    //按ID删除
    @ApiOperation(value = "通过ID删除" )
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable Serializable id){
        try {
            menuService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }

    @Permission(name = "批量删除" ,descs = "批量删除")
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            menuService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @Permission(name = "角色的分页查询",descs = "分页查询")
    @PostMapping
    public AjaxResult pageList(@RequestBody MenuQuery menuQuery) {
        PageList<Menu> pageList =  menuService.pageList(menuQuery);
        return AjaxResult.me().setData(pageList);
    }

    //只能查一级菜单
    @GetMapping("/tree/{loginUserId}")
    public AjaxResult tree(@PathVariable("loginUserId") Long loginUserId) {
        List<Menu> menus = menuService.tree(loginUserId);
        return AjaxResult.me().setData(menus);
    }

    @GetMapping("/getTree")
    public AjaxResult getTree() {
        List<Menu> menus = menuService.getTree();
        return AjaxResult.me().setData(menus);
    }
}
